/**
 * 制作航路计划
 * @author williamJM
 * date 2018/05/08 14:52
 */

"use strict";

define(["app","config/pathConfig","layer"],function (app,pathConfig,layer) {
    app.registerController("ctrl.route.editRoute",function ($scope,$http,airportComponent) {

        //初始化机场选择组件
        airportComponent.initAirportAssembly($scope,$http,pathConfig);

        $scope.sid = "-1";
        $scope.star = "-1";
        $scope.starIsDisabled = true;
        $scope.sidIsDisabled = true;
        $scope.sidApproachLeg = null;
        $scope.route = [];

        var getLeaveRwyInfo = function(runwayEndId) {
          for(var i = 0; i < $scope.LevRnwayList.length; i++) {
              if($scope.LevRnwayList[i].navRunwayEnd.runwayEndId === runwayEndId) {
                  return $scope.LevRnwayList[i];
              }
          }
        };

        $scope.$watch("leaveRwy",function (value) {
            if(value !== "" && value !== "-1") {
                $scope.sidIsDisabled = false;
                getSid(Number(value));
                var leaveRwyInfo = getLeaveRwyInfo(Number(value));
                var leaveRwyInfoPoint = [leaveRwyInfo.navRunwayEnd.lonx,leaveRwyInfo.navRunwayEnd.laty];
                $scope.route.push({type : "RWY",point : leaveRwyInfoPoint});
            } else {
                $scope.sidIsDisabled = true;
                $scope.sid = "-1";
            }
        });


        $scope.$watch("arrivalRwy",function (value) {
            if(value !== "" && value !== "-1") {
                $scope.starIsDisabled = false;
                getSTAR($scope.currentArrAirport.airportId);
            } else {
                $scope.starIsDisabled = true
            }
        });

        var getApproachByRunwayEndId = function(runwayEndId) {
            return new Promise(function (success,error) {
                $http.get(pathConfig.backstagePath.getPath() + "/approachLeg/approach/" + runwayEndId)
                    .then(function successCallback(response) {
                        success(response.data.data);
                    });
            });
        };

        var appApproachData = function(data) {
            var $approachLeg = $(".approach-leg");
            $approachLeg.empty();
            for(var i = 0; i < data.length; i++) {
                $approachLeg.append("<tr>" +
                    "<td>10</td>" +
                    "<td>22</td>" +
                    "<td>33</td>" +
                    "<td>"+ data[i].fixIdent +"</td>" +
                    "<td></td>" +
                    "</tr>");
            }
        };

        $scope.$watch("sid",function (approachId) {
            if(approachId !== "" && approachId !== "-1") {
                getApproachByRunwayEndId(approachId).then(function success(data) {
                    appApproachData(data);
                })
            }
        });

        //查找标准离场程序
        var getSid = function (rwyEndId) {
            $http.get(pathConfig.backstagePath.getPath() + "/approach/"+rwyEndId+"/sid")
                .then(function successCallback(response) {
                    $scope.sids = response.data.data;
                    $scope.sid = "-1";
                })
        };

        //查找标准进场程序
        var getSTAR = function (airportId) {
            $http.get(pathConfig.backstagePath.getPath() + "/approach/"+airportId+"/star")
                .then(function successCallback(response) {
                    $scope.stars = response.data.data;
                    $scope.star = "-1";
                })
        };

        //监听航路数据变化
        $scope.$watch("route",function (value) {
           console.log(value);
        });


        $scope.$watch("currentLeaveAirport",function (airport){

        });


        //获取map iframe
        $scope.$watch("mapIframe",function (mapIframe) {
            if(mapIframe !== undefined) {
                var pointGraphic = null;
                $scope.$watch("currentLeaveAirport",function (airport) {
                    if(airport.lonx && airport.laty) {
                        $scope.route.push({type : "AP",point : [airport.lonx,airport.laty]})
                        //pointGraphic = mapIframe.mapService.dragPoint([airport.lonx,airport.laty]);
                    }
                });


                $scope.$watch("currentArrAirport",function(airport){
                    if(airport.lonx && airport.laty) {
                       var graphic = mapIframe.mapService.dragPoint([airport.lonx,airport.laty]);
                        mapIframe.mapService.reDragPoint(graphic,[airport.lonx,airport.laty]);
                    }
                })

            }
        })


    })
    
});
